package com.success.toutiao;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;

import java.util.Set;

/**
 * Desc: 2018.12.19
 * cp头条面试，输出两个JSON对象不一样的地方
 * Create by John Jiang On 2018/12/20
 */
public class DiffJSON {

    public void printDiffJSON(JSON oa, JSON ob) {
        if (oa == null) {
            System.out.println(ob);
        }
        if (ob == null) {
            System.out.println(oa);
        }
        if (oa instanceof JSONArray && ob instanceof JSONArray) {
            printDiffJSON(oa, ob);
            return;
        } else if (oa instanceof JSONObject && ob instanceof JSONObject) {
            JSONObject a = (JSONObject) oa;
            JSONObject b = (JSONObject) ob;
            Set<String> aKeys = a.keySet();
            Set<String> bKeys = b.keySet();
            for (String key : aKeys) {
                if (!bKeys.contains(key)) {
                    System.out.println(key + ":"+a.get(key));
                } else {
                    Object objectA = a.get(key);
                    Object objectB = b.get(key);
                    printObject(objectA, objectB);
                }
            }
            for (String key : bKeys) {
                if (!aKeys.contains(key)) {
                    System.out.println(key + ":" + b.get(key));
                }
            }
        }
    }

    private void arrayDiff(JSONArray oa, JSONArray ob) {
        if (oa.size() != ob.size()) {
            System.out.println(oa.toJSONString());
            System.out.println(ob.toJSONString());
        } else {
            for (int i = 0; i < oa.size(); i++) {
                Object objectA = oa.get(i);
                Object objectB = ob.get(i);
                printObject(objectA, objectB);
            }
        }
    }

    private void printObject(Object objectA, Object objectB) {
        if (objectA instanceof Integer && objectB instanceof Integer) {
            if (!objectA.equals(objectB)) {
                System.out.println(objectA);
                System.out.println(objectB);
            }
        } else if (objectA instanceof Double && objectB instanceof Double) {
            if (!objectA.equals(objectB)) {
                System.out.println(objectA);
                System.out.println(objectB);
            }
        } else if (objectA instanceof Long && objectB instanceof Long) {
            if (!objectA.equals(objectB)) {
                System.out.println(objectA);
                System.out.println(objectB);
            }
        } else if (objectA instanceof String && objectB instanceof String) {
            if (!objectA.equals(objectB)) {
                System.out.println(objectA);
                System.out.println(objectB);
            }
        } else if (objectA instanceof JSONArray && objectB instanceof JSONArray) {
            arrayDiff((JSONArray) objectA, (JSONArray) objectB);
        } else if (objectA instanceof JSONObject && objectB instanceof JSONObject) {
            printDiffJSON((JSONObject) objectA, (JSONObject) objectB);
        } else {
            System.out.println(objectA.toString());
            System.out.println(objectB.toString());
        }
    }

    @Test
    public void test() {
        String a = "{\"data\":{\"pageNum1\":1,\"pageSize\":10,\"pages\":8,\"total\":77,\"list\":[{\"name\":\"平安金 5克貔貅金币1\",\"imgUrl\":\"https://img.itaojin8.cn/product/award/1535098112380_1.png\",\"type\":\"MATERIALOBJECT\",\"subType\":\"MATERIALOBJECT\",\"price\":15800000,\"weight\":9999,\"productInventory\":{\"num\":579,\"exchangeableNum\":5,\"exchangedNumToday\":0,\"product\":{\"id\":68},\"id\":68,\"created\":1531985732000,\"updated\":1539078668919},\"startTime\":1535098107000,\"endTime\":1540785600000,\"detailUrl\":\"https://m1.itaojin8.com/nfop/tttaojin/5g/index.htm\",\"id\":68,\"created\":1531985732000,\"updated\":1540782620872},{\"name\":\"iPhone8 64G\",\"imgUrl\":\"https://img.itaojin8.cn/product/award/1526638944024_1.jpg\",\"type\":\"MATERIALOBJECT\",\"subType\":\"MATERIALOBJECT\",\"price\":47900000,\"weight\":9800,\"productInventory\":{\"num\":392,\"exchangeableNum\":30,\"exchangedNumToday\":0,\"product\":{\"id\":3},\"id\":3,\"created\":1508307461000,\"updated\":1534852159216},\"startTime\":1508307424000,\"endTime\":1540785600000,\"detailUrl\":\"https://m1.itaojin8.com/nfop/tttaojin/iphone8/index.htm\",\"id\":3,\"created\":1508307461000,\"updated\":1540782620872},{\"name\":\"888元彩票红包\",\"imgUrl\":\"https://img.itaojin8.cn/product/award/1526639019641_1.jpg\",\"type\":\"VIRTUAL\",\"subType\":\"REDEEM_CODE\",\"price\":9180000,\"weight\":9600,\"productInventory\":{\"num\":0,\"exchangeableNum\":30,\"exchangedNumToday\":0,\"product\":{\"id\":7},\"id\":7,\"created\":1508307882000,\"updated\":1536474046370},\"startTime\":1508307869000,\"endTime\":1540785600000,\"id\":7,\"created\":1508307882000,\"updated\":1540782620872},{\"name\":\"888元 彩票红包\",\"imgUrl\":\"https://img.itaojin8.cn/product/award/1526639029471_1.jpg\",\"type\":\"VIRTUAL\",\"subType\":\"REDEEM_CODE\",\"price\":8980000,\"weight\":9600,\"productInventory\":{\"num\":24,\"exchangeableNum\":20,\"exchangedNumToday\":0,\"product\":{\"id\":20},\"id\":20,\"created\":1511426698000,\"updated\":1537367696668},\"startTime\":1511426691000,\"endTime\":1540785600000,\"id\":20,\"created\":1511426698000,\"updated\":1540782620872},{\"name\":\"黄金貔貅 转运手链\",\"imgUrl\":\"https://img.itaojin8.cn/product/award/1526638463466_1.jpg\",\"type\":\"MATERIALOBJECT\",\"subType\":\"MATERIALOBJECT\",\"price\":2080000,\"weight\":9500,\"productInventory\":{\"num\":444,\"exchangeableNum\":50,\"exchangedNumToday\":0,\"product\":{\"id\":16},\"id\":16,\"created\":1509950814000,\"updated\":1538730342457},\"startTime\":1509950805000,\"endTime\":1540785600000,\"detailUrl\":\"https://m1.itaojin8.com/nfop/tttaojin/pixiu2/index.htm\",\"id\":16,\"created\":1509950814000,\"updated\":1540782620872},{\"name\":\"长城红酒 3年酿(6瓶)\",\"imgUrl\":\"https://img.itaojin8.cn/product/award/1536218404979_1.png\",\"type\":\"MATERIALOBJECT\",\"subType\":\"MATERIALOBJECT\",\"price\":1880000,\"weight\":9250,\"productInventory\":{\"num\":83,\"exchangeableNum\":10,\"exchangedNumToday\":0,\"product\":{\"id\":75},\"id\":75,\"created\":1536218429000,\"updated\":1538221141605},\"startTime\":1536218399000,\"endTime\":1540785600000,\"detailUrl\":\"https://m1.itaojin8.com/nfop/tttaojin/hongjiu/index.htm\",\"id\":75,\"created\":1536218429000,\"updated\":1540782620872},{\"name\":\"红牛24罐\",\"imgUrl\":\"https://img.itaojin8.cn/product/award/1526638439449_1.png\",\"type\":\"MATERIALOBJECT\",\"subType\":\"MATERIALOBJECT\",\"price\":1500000,\"weight\":9200,\"productInventory\":{\"num\":288,\"exchangeableNum\":50,\"exchangedNumToday\":0,\"product\":{\"id\":38},\"id\":38,\"created\":1521172045000,\"updated\":1540559805569},\"startTime\":1521172038000,\"endTime\":1540785600000,\"detailUrl\":\"https://m1.itaojin8.com/nfop/tttaojin/redbull/index.htm\",\"id\":38,\"created\":1521172045000,\"updated\":1540782561069},{\"name\":\"电信100元充值卡\",\"imgUrl\":\"https://img.itaojin8.cn/product/award/1525775046614_1.jpg\",\"type\":\"VIRTUAL\",\"subType\":\"VIRTUAL_CARD\",\"price\":1100000,\"weight\":9180,\"productInventory\":{\"num\":3775,\"exchangeableNum\":800,\"exchangedNumToday\":0,\"product\":{\"id\":11},\"id\":11,\"created\":1508310739000,\"updated\":1540436444615},\"startTime\":1508310718000,\"endTime\":1540785600000,\"detailUrl\":\"https://m1.itaojin8.com/nfop/tttaojin/10086/index.htm\",\"id\":11,\"created\":1508310739000,\"updated\":1540782620872},{\"name\":\"一大箱零食30种\",\"imgUrl\":\"https://img.itaojin8.cn/product/award/1528450458137_1.png\",\"type\":\"MATERIALOBJECT\",\"subType\":\"MATERIALOBJECT\",\"price\":920000,\"weight\":9160,\"productInventory\":{\"num\":304,\"exchangeableNum\":50,\"exchangedNumToday\":0,\"product\":{\"id\":52},\"id\":52,\"created\":1527669381000,\"updated\":1540626996387},\"startTime\":1527669372000,\"endTime\":1540785600000,\"detailUrl\":\"https://m1.itaojin8.com/nfop/tttaojin/lingshilibao/index.htm\",\"id\":52,\"created\":1527669381000,\"updated\":1540782529071},{\"name\":\"小米手环3\",\"imgUrl\":\"https://img.itaojin8.cn/product/award/1531823955725_1.png\",\"type\":\"MATERIALOBJECT\",\"subType\":\"MATERIALOBJECT\",\"price\":1790000,\"weight\":9158,\"productInventory\":{\"num\":30,\"exchangeableNum\":20,\"exchangedNumToday\":0,\"product\":{\"id\":54},\"id\":54,\"created\":1527669502000,\"updated\":1539258050675},\"startTime\":1527669476000,\"endTime\":1540785600000,\"detailUrl\":\"https://m1.itaojin8.com/nfop/tttaojin/shouhuan/index.htm\",\"id\":54,\"created\":1527669502000,\"updated\":1540782620872}]},\"code\":200}";
        String b = "{\"data\":{\"pageNum\":1,\"pageSize\":10,\"pages\":8,\"total\":77,\"list\":[{\"name\":\"平安金 5克貔貅金币\",\"imgUrl\":\"https://img.itaojin8.cn/product/award/1535098112380_1.png\",\"type\":\"MATERIALOBJECT\",\"subType\":\"MATERIALOBJECT\",\"price\":15800000,\"weight\":9999,\"productInventory\":{\"num\":579,\"exchangeableNum\":5,\"exchangedNumToday\":0,\"product\":{\"id\":68},\"id\":68,\"created\":1531985732000,\"updated\":1539078668919},\"startTime\":1535098107000,\"endTime\":1540785600000,\"detailUrl\":\"https://m1.itaojin8.com/nfop/tttaojin/5g/index.htm\",\"id\":68,\"created\":1531985732000,\"updated\":1540782620872},{\"name\":\"iPhone8 64G\",\"imgUrl\":\"https://img.itaojin8.cn/product/award/1526638944024_1.jpg\",\"type\":\"MATERIALOBJECT\",\"subType\":\"MATERIALOBJECT\",\"price\":47900000,\"weight\":9800,\"productInventory\":{\"num\":392,\"exchangeableNum\":30,\"exchangedNumToday\":0,\"product\":{\"id\":3},\"id\":3,\"created\":1508307461000,\"updated\":1534852159216},\"startTime\":1508307424000,\"endTime\":1540785600000,\"detailUrl\":\"https://m1.itaojin8.com/nfop/tttaojin/iphone8/index.htm\",\"id\":3,\"created\":1508307461000,\"updated\":1540782620872},{\"name\":\"888元彩票红包\",\"imgUrl\":\"https://img.itaojin8.cn/product/award/1526639019641_1.jpg\",\"type\":\"VIRTUAL\",\"subType\":\"REDEEM_CODE\",\"price\":9180000,\"weight\":9600,\"productInventory\":{\"num\":0,\"exchangeableNum\":30,\"exchangedNumToday\":0,\"product\":{\"id\":7},\"id\":7,\"created\":1508307882000,\"updated\":1536474046370},\"startTime\":1508307869000,\"endTime\":1540785600000,\"id\":7,\"created\":1508307882000,\"updated\":1540782620872},{\"name\":\"888元 彩票红包\",\"imgUrl\":\"https://img.itaojin8.cn/product/award/1526639029471_1.jpg\",\"type\":\"VIRTUAL\",\"subType\":\"REDEEM_CODE\",\"price\":8980000,\"weight\":9600,\"productInventory\":{\"num\":24,\"exchangeableNum\":20,\"exchangedNumToday\":0,\"product\":{\"id\":20},\"id\":20,\"created\":1511426698000,\"updated\":1537367696668},\"startTime\":1511426691000,\"endTime\":1540785600000,\"id\":20,\"created\":1511426698000,\"updated\":1540782620872},{\"name\":\"黄金貔貅 转运手链\",\"imgUrl\":\"https://img.itaojin8.cn/product/award/1526638463466_1.jpg\",\"type\":\"MATERIALOBJECT\",\"subType\":\"MATERIALOBJECT\",\"price\":2080000,\"weight\":9500,\"productInventory\":{\"num\":444,\"exchangeableNum\":50,\"exchangedNumToday\":0,\"product\":{\"id\":16},\"id\":16,\"created\":1509950814000,\"updated\":1538730342457},\"startTime\":1509950805000,\"endTime\":1540785600000,\"detailUrl\":\"https://m1.itaojin8.com/nfop/tttaojin/pixiu2/index.htm\",\"id\":16,\"created\":1509950814000,\"updated\":1540782620872},{\"name\":\"长城红酒 3年酿(6瓶)\",\"imgUrl\":\"https://img.itaojin8.cn/product/award/1536218404979_1.png\",\"type\":\"MATERIALOBJECT\",\"subType\":\"MATERIALOBJECT\",\"price\":1880000,\"weight\":9250,\"productInventory\":{\"num\":83,\"exchangeableNum\":10,\"exchangedNumToday\":0,\"product\":{\"id\":75},\"id\":75,\"created\":1536218429000,\"updated\":1538221141605},\"startTime\":1536218399000,\"endTime\":1540785600000,\"detailUrl\":\"https://m1.itaojin8.com/nfop/tttaojin/hongjiu/index.htm\",\"id\":75,\"created\":1536218429000,\"updated\":1540782620872},{\"name\":\"红牛24罐\",\"imgUrl\":\"https://img.itaojin8.cn/product/award/1526638439449_1.png\",\"type\":\"MATERIALOBJECT\",\"subType\":\"MATERIALOBJECT\",\"price\":1500000,\"weight\":9200,\"productInventory\":{\"num\":288,\"exchangeableNum\":50,\"exchangedNumToday\":0,\"product\":{\"id\":38},\"id\":38,\"created\":1521172045000,\"updated\":1540559805569},\"startTime\":1521172038000,\"endTime\":1540785600000,\"detailUrl\":\"https://m1.itaojin8.com/nfop/tttaojin/redbull/index.htm\",\"id\":38,\"created\":1521172045000,\"updated\":1540782561069},{\"name\":\"电信100元充值卡\",\"imgUrl\":\"https://img.itaojin8.cn/product/award/1525775046614_1.jpg\",\"type\":\"VIRTUAL\",\"subType\":\"VIRTUAL_CARD\",\"price\":1100000,\"weight\":9180,\"productInventory\":{\"num\":3775,\"exchangeableNum\":800,\"exchangedNumToday\":0,\"product\":{\"id\":11},\"id\":11,\"created\":1508310739000,\"updated\":1540436444615},\"startTime\":1508310718000,\"endTime\":1540785600000,\"detailUrl\":\"https://m1.itaojin8.com/nfop/tttaojin/10086/index.htm\",\"id\":11,\"created\":1508310739000,\"updated\":1540782620872},{\"name\":\"一大箱零食30种\",\"imgUrl\":\"https://img.itaojin8.cn/product/award/1528450458137_1.png\",\"type\":\"MATERIALOBJECT\",\"subType\":\"MATERIALOBJECT\",\"price\":920000,\"weight\":9160,\"productInventory\":{\"num\":304,\"exchangeableNum\":50,\"exchangedNumToday\":0,\"product\":{\"id\":52},\"id\":52,\"created\":1527669381000,\"updated\":1540626996387},\"startTime\":1527669372000,\"endTime\":1540785600000,\"detailUrl\":\"https://m1.itaojin8.com/nfop/tttaojin/lingshilibao/index.htm\",\"id\":52,\"created\":1527669381000,\"updated\":1540782529071},{\"name\":\"小米手环3\",\"imgUrl\":\"https://img.itaojin8.cn/product/award/1531823955725_1.png\",\"type\":\"MATERIALOBJECT\",\"subType\":\"MATERIALOBJECT\",\"price\":1790000,\"weight\":9158,\"productInventory\":{\"num\":30,\"exchangeableNum\":20,\"exchangedNumToday\":0,\"product\":{\"id\":54},\"id\":54,\"created\":1527669502000,\"updated\":1539258050675},\"startTime\":1527669476000,\"endTime\":1540785600000,\"detailUrl\":\"https://m1.itaojin8.com/nfop/tttaojin/shouhuan/index.htm\",\"id\":54,\"created\":1527669502000,\"updated\":1540782620872}]},\"code\":200}";
        printDiffJSON(JSON.parseObject(a), JSON.parseObject(b));
    }

}
